package com.tuniu.tabFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.banner.BannerHomeFragment;
import com.meetme.android.horizontallistview.HorizontalListView;
import com.tuniu.R;
import com.tuniu.activity.DetailGenTuanActivity;
import com.tuniu.activity.InterestFragment;
import com.tuniu.data.home.Ad;
import com.tuniu.data.home.Advertise;
import com.tuniu.data.home.CategoryNew;
import com.tuniu.data.home.Content_;
import com.tuniu.data.home.Data;
import com.tuniu.data.home.FindBeauty;
import com.tuniu.data.home.FindBeautyRight;
import com.tuniu.data.home.HomeBanner;
import com.tuniu.data.home.HotRecommend;
import com.tuniu.data.home.LoginGift;
import com.tuniu.data.home.Module;
import com.tuniu.data.home.OfflineInfo;
import com.tuniu.data.home.RecommendForYou;
import com.tuniu.data.home.Registration;
import com.tuniu.data.home.RegistrationLeft;
import com.tuniu.data.home.RegistrationRight;
import com.tuniu.data.home.Service;
import com.tuniu.data.home.Service_;
import com.tuniu.data.home.SignIn;
import com.tuniu.data.home.Specials;
import com.tuniu.data.interesting.Content;
import com.tuniu.data.interesting.DestRecommend;
import com.tuniu.data.interesting.DestinationTatal;
import com.tuniu.data.interesting.ProductList;
import com.tuniu.data.interesting.RecommendRoutes;
import com.tuniu.data.product.BrandLabel;
import com.tuniu.data.product.Product;
import com.tuniu.data.product.Product_;
import com.tuniu.utils.Constants;
import com.tuniu.utils.MyAPIUtils;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.HTTPUtils;
import com.xinbo.utils.ResponseListener;
import com.xinbo.utils.UILUtils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import cn.iwgang.countdownview.CountdownView;

/**
 * A simple {@link Fragment} subclass.
 *
 */
@SuppressLint("ViewHolder")
public class HomeFragment extends Fragment {

	private View view;
	private LayoutInflater inflater;
	private ListView mListView;
	private MyAdapter myAdapter;

	private ArrayList<Product_> productLists = new ArrayList<Product_>();
	private ArrayList<Advertise> advertisesList = new ArrayList<Advertise>();
	private ArrayList<CategoryNew> categoryNewsList = new ArrayList<CategoryNew>();
	private ArrayList<Ad> adLists = new ArrayList<Ad>();
	private ArrayList<RecommendForYou> recommendForYouList = new ArrayList<RecommendForYou>();
	private ArrayList<Module> modulesList = new ArrayList<Module>();
	private ArrayList<com.tuniu.data.interesting.Content_> content_i = new ArrayList<com.tuniu.data.interesting.Content_>();
	private ArrayList<Content> contentList = new ArrayList<Content>();
	private ViewPager mBannerPager;
	private MyPagerAdpter myPagerAdpter;
	private ViewFlipper mFlipper;
	private LinearLayout hidden;

	public HomeFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		if (view == null) {
			view = inflater.inflate(R.layout.fragment_home, container, false);
			initUI();
			initData();
		}
		return view;
	}

	// 数据请求
	private void initData() {
		// 首页数据
		initBanner();
		// 游玩数据
		initProduct();
	}

	private MyAdapter myIAdapter;

	private void initInterest(final int position) {
		HTTPUtils.get(getContext(), Constants.url.INTETEST, new ResponseListener() {
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				DestinationTatal tatal = GsonUtils.parseJSON(arg0, DestinationTatal.class);
				com.tuniu.data.interesting.Data data = tatal.getData();
				RecommendRoutes routes = data.getRecommendRoutes();
				DestRecommend destRecommend = data.getDestRecommend();
				List<Content> content = destRecommend.getContent();
				contentList.clear();
				contentList.addAll(content);
				List<com.tuniu.data.interesting.Content_> contents = routes.getContents();
				content_i.clear();
				content_i.addAll(contents);
				if (contents != null) {
					for (int i = 0; i < contents.size(); i++) {
						if (i == 0) {
							if (contents.get(0).getTitle() != null) {
								btn_content1.setText(contents.get(0).getTitle());
							} else {
								btn_content1.setVisibility(View.GONE);
							}
						} else if (i == 1) {
							if (contents.get(i).getTitle() != null) {
								btn_content2.setText(contents.get(i).getTitle());
							} else {
								btn_content2.setVisibility(View.GONE);
							}
						} else if (i == 2) {
							if (contents.get(i).getTitle() != null) {
								btn_content3.setText(contents.get(i).getTitle());
							} else {
								btn_content3.setVisibility(View.GONE);
							}
						}
					}
				}

				com.tuniu.data.interesting.Content_ content_ = contents.get(position);
				List<ProductList> productList = content_.getProductList();
				productListsI.clear();
				productListsI.addAll(productList);

				// TODO 刷新
				myIAdapter.notifyDataSetChanged();
				myIViewPagerAdapter.notifyDataSetChanged();
			}

			@Override
			public void onErrorResponse(VolleyError arg0) {

			}
		});

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return productListsI.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			View view = null;
			if (convertView == null) {
				view = inflater.inflate(R.layout.item_hlvlistview, null);
				holder = new Holder();
				holder.imagUrl = (ImageView) view.findViewById(R.id.imagUrl);
				holder.departCityName = (TextView) view.findViewById(R.id.departCityName);
				holder.textproductName = (TextView) view.findViewById(R.id.textproductName);
				holder.productTypeName = (TextView) view.findViewById(R.id.productTypeName);
				holder.price = (TextView) view.findViewById(R.id.price);
				view.setTag(holder);
			} else {
				view = convertView;
				holder = (Holder) view.getTag();
			}

			ProductList list = productListsI.get(position);
			String imgUrl = list.getImgUrl();
			int price2 = list.getPrice();
			String departCityName2 = list.getDepartCityName();
			String productName = list.getProductName();
			String productTypeName2 = list.getProductTypeName();
			UILUtils.displayImage(imgUrl, holder.imagUrl);
			holder.departCityName.setText(departCityName2);
			holder.textproductName.setText(productName);
			holder.productTypeName.setText(productTypeName2);
			holder.price.setText("¥" + price2 + "");
			return view;
		}
	}

	private void initHeadView8() {
		View layout = inflater.inflate(R.layout.home_zuijinguanzhu, null);
		HorizontalListView mHorizontalListView = (HorizontalListView) layout.findViewById(R.id.hlvCustomList);
		myIAdapter = new MyAdapter();

		btn_content1 = (Button) layout.findViewById(R.id.btn_content1);
		btn_content2 = (Button) layout.findViewById(R.id.btn_content2);
		btn_content3 = (Button) layout.findViewById(R.id.btn_content3);
		btn_content1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				btn_content1.setTextColor(0xff28CD7A);
				btn_content2.setTextColor(0xff737373);
				btn_content3.setTextColor(0xff737373);
				initInterest(0);

			}
		});
		btn_content2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				btn_content1.setTextColor(0xff737373);
				btn_content2.setTextColor(0xff28CD7A);
				btn_content3.setTextColor(0xff737373);
				initInterest(1);

			}
		});
		btn_content3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				btn_content1.setTextColor(0xff737373);
				btn_content2.setTextColor(0xff737373);
				btn_content3.setTextColor(0xff28CD7A);
				initInterest(2);

			}
		});
		mListView.addHeaderView(layout);
		mHorizontalListView.setAdapter(myIAdapter);
	}

	class Holder {
		public TextView textView;
		public ImageView imagUrl;
		public TextView departCityName;
		public TextView textproductName;
		public TextView productTypeName;
		public TextView price;
	}

	private void dangji(final int position) {
		HTTPUtils.get(getContext(), Constants.url.BANNER, new ResponseListener() {

			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				HomeBanner banner = GsonUtils.parseJSON(arg0, HomeBanner.class);
				Data data = banner.getData();

				// 当季推荐 出境游 国内游 国际游
				List<RecommendForYou> recommendForYou = data.getRecommendForYou();
				recommendForYouList.clear();
				recommendForYouList.addAll(recommendForYou);
				// 当季推荐 出境游 国内游 国际游
				for (int i = 0; i < recommendForYouList.size(); i++) {
					if (i == 0) {
						recommendForYou_content1.setText(recommendForYouList.get(i).getTitle());
						recommendForYou_content11.setText(recommendForYouList.get(i).getTitle());
					} else if (i == 1) {
						recommendForYou_content2.setText(recommendForYouList.get(i).getTitle());
						recommendForYou_content22.setText(recommendForYouList.get(i).getTitle());
					} else if (i == 2) {
						recommendForYou_content3.setText(recommendForYouList.get(i).getTitle());
						recommendForYou_content33.setText(recommendForYouList.get(i).getTitle());
					} else if (i == 3) {
						recommendForYou_content4.setText(recommendForYouList.get(i).getTitle());
						recommendForYou_content44.setText(recommendForYouList.get(i).getTitle());
					}
				}
				List<Module> modules = recommendForYouList.get(position).getModules();
				modulesList.clear();
				modulesList.addAll(modules);

				dangjiAdapter.notifyDataSetChanged();
				layououtOver.notifyDataSetChanged();
			}

			@Override
			public void onErrorResponse(VolleyError arg0) {

			}
		});

	}

	private void initBanner() {
		
		MyAPIUtils.getBanner(getContext(), new ResponseListener() {
			
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				HomeBanner banner = GsonUtils.parseJSON(arg0, HomeBanner.class);
				Data data = banner.getData();
				// banner
				List<Advertise> advertises = data.getAdvertises();
				// 模块
				List<CategoryNew> categoryNew = data.getCategoryNew();

				categoryNewsList.clear();
				categoryNewsList.addAll(categoryNew);
				advertisesList.clear();
				advertisesList.addAll(advertises);
				initHeadView1();
				initHeadView2();
				initHeadView3();

				// 签到夺宝
				addFlipper(data);
				// 手机专区
				specials(data);
				// 登录有礼
				logingift(data);
				// 热门推荐
				hot(data);

				// TODO 你可能感兴趣的目的地

				initeresting();
				// 发现精彩
				found(data);
				// 营业门店
				offlineInfo(data);
				// 最近关注
				initHeadView8();
				dangji(0);
				layououtOver();
				initHeadView13();
				mListView.setAdapter(myAdapter);
				myPagerAdpter.notifyDataSetChanged();
				myAdapter.notifyDataSetChanged();

				initInterest(0);
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	
	}

	private void initProduct() {
		
		MyAPIUtils.getProduct(getContext(), new ResponseListener() {
			
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				Product product = GsonUtils.parseJSON(arg0, Product.class);
				Data data = product.getData();
				List<Product_> products = data.getProducts();
				productLists.clear();
				productLists.addAll(products);

				myAdapter.notifyDataSetChanged();
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	}

	private void initUI() {
		mListView = (ListView) view.findViewById(R.id.listView_home);
		// Listview适配器
		productAdapter();
		// 添加HeadView
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//后续实现序列化接口
				Intent intent=new Intent(getContext(), DetailGenTuanActivity.class);
				int headerViewsCount = mListView.getHeaderViewsCount();
				String productId = productLists.get(position-headerViewsCount).getProductId();
				intent.putExtra("productId", productId);
				startActivity(intent);
			}
		});
		
	}

	ArrayList<BrandLabel> labelsList = new ArrayList<BrandLabel>();

	private void productAdapter() {
		myAdapter = new MyAdapter() {
			@SuppressLint("NewApi")
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View layout = null;
				if (convertView == null) {
					layout = inflater.inflate(R.layout.item_product, null);
				} else {
					layout = convertView;
				}
				ImageView productImage = (ImageView) layout.findViewById(R.id.productImage);
				ImageView iv_iconType = (ImageView) layout.findViewById(R.id.iv_iconType);
				TextView productTypeName = (TextView) layout.findViewById(R.id.productTypeName);
				TextView departCityName = (TextView) layout.findViewById(R.id.departCityName);
				TextView promotionPrice = (TextView) layout.findViewById(R.id.promotionPrice);
				TextView promotionDesc = (TextView) layout.findViewById(R.id.promotionDesc);
				TextView peopleCount = (TextView) layout.findViewById(R.id.peopleCount);
				TextView satisfaction = (TextView) layout.findViewById(R.id.satisfaction);
				TextView product_title = (TextView) layout.findViewById(R.id.product_title);
				TextView product_subTitle = (TextView) layout.findViewById(R.id.product_subTitle);
				TextView brandLabels_title1 = (TextView) layout.findViewById(R.id.brandLabels_title1);
				TextView brandLabels_title2 = (TextView) layout.findViewById(R.id.brandLabels_title2);
				TextView brandLabels_title3 = (TextView) layout.findViewById(R.id.brandLabels_title3);
				TextView brandLabels_title4 = (TextView) layout.findViewById(R.id.brandLabels_title4);
				TextView brandLabels_title5 = (TextView) layout.findViewById(R.id.brandLabels_title5);
				Product_ product_ = productLists.get(position);
				List<BrandLabel> brandLabels = product_.getBrandLabels();
				labelsList.clear();
				labelsList.addAll(brandLabels);
				UILUtils.displayImage(product_.getImgUrl(), productImage);
				int iconType = product_.getIconType();
				if (iconType == 1) {
					iv_iconType.setImageResource(R.drawable.icon_home_page_product_group);
				} else if (iconType == 2) {
					iv_iconType.setImageResource(R.drawable.icon_home_page_product_selfhelp);
				}
				productTypeName.setText(product_.getProductTypeName());
				departCityName.setText(product_.getDepartCityName() + "出发");
				promotionPrice.setText(product_.getPromotionPrice() + "");
				if (product_.getPromotionDesc().equals("")) {
					promotionDesc.setVisibility(View.GONE);
					promotionDesc.setBackground(null);
				} else {
					promotionDesc.setVisibility(View.VISIBLE);
					promotionDesc.setText(product_.getPromotionDesc());
					promotionDesc.setBackgroundResource(R.drawable.shape_promotiondesc);
				}
				peopleCount.setText(product_.getPeopleCount() + "人出游");
				satisfaction.setText(product_.getSatisfaction() + "满意");
				product_title.setText(product_.getTitle());
				product_subTitle.setText(product_.getSubTitle());

				for (int i = 0; i < labelsList.size(); i++) {
					if (i == 0) {
						if (labelsList.get(i).getTitle().contains("立减")) {
							brandLabels_title1.setTextColor(0xffFA7864);
						} else if (labelsList.get(i).getTitle().contains("牛人专线")) {
							brandLabels_title1.setTextColor(0xffeeaa44);
						} else {
							brandLabels_title1.setTextColor(0xff9B9B9B);
						}
						brandLabels_title1.setText(labelsList.get(i).getTitle());
					} else if (i == 1) {
						if (labelsList.get(i).getTitle().contains("牛人专线")) {
							brandLabels_title2.setTextColor(0xffeeaa44);
						} else {
							brandLabels_title2.setTextColor(0xff9B9B9B);
						}
						brandLabels_title2.setText(labelsList.get(i).getTitle());
					} else if (i == 2) {
						brandLabels_title3.setText(labelsList.get(i).getTitle());
					} else if (i == 3) {
						brandLabels_title4.setText(labelsList.get(i).getTitle());
					} else if (i == 4) {
						brandLabels_title5.setText(labelsList.get(i).getTitle());
					}

				}
				return layout;
			}

			@Override
			public int getCount() {
				return productLists.size();
			}
		};

	}

	private void initHeadView1() {
		// 自己创建一个viewPager+Indicator的布局
		View view = inflater.inflate(R.layout.headview_banner, null);
		mBannerPager = (ViewPager) view.findViewById(R.id.bannerPager);
		myPagerAdpter = new MyPagerAdpter(getChildFragmentManager());
		mBannerPager.setAdapter(myPagerAdpter);
		mListView.addHeaderView(view);
	}

	private void initHeadView2() {
		View view = inflater.inflate(R.layout.home_headview2, null);
		ImageView lvyou = (ImageView) view.findViewById(R.id.iv_lvyou);
		ImageView iv_chujinyou = (ImageView) view.findViewById(R.id.iv_chujinyou);

		mListView.addHeaderView(view);
	}

	private void initHeadView3() {
		View view = inflater.inflate(R.layout.home_headview3, null);
		ImageView iv_smallCategory1 = (ImageView) view.findViewById(R.id.iv_smallCategory1);
		ImageView iv_smallCategory2 = (ImageView) view.findViewById(R.id.iv_smallCategory2);
		ImageView iv_smallCategory3 = (ImageView) view.findViewById(R.id.iv_smallCategory3);
		ImageView iv_smallCategory4 = (ImageView) view.findViewById(R.id.iv_smallCategory4);
		ImageView iv_smallCategory5 = (ImageView) view.findViewById(R.id.iv_smallCategory5);
		TextView tv_smallCategory1 = (TextView) view.findViewById(R.id.tv_smallCategory1);
		TextView tv_smallCategory2 = (TextView) view.findViewById(R.id.tv_smallCategory2);
		TextView tv_smallCategory3 = (TextView) view.findViewById(R.id.tv_smallCategory3);
		TextView tv_smallCategory4 = (TextView) view.findViewById(R.id.tv_smallCategory4);
		TextView tv_smallCategory5 = (TextView) view.findViewById(R.id.tv_smallCategory5);
		mListView.addHeaderView(view);
	}

	// initHeadView4
	private void addFlipper(Data data) {
		View layout = inflater.inflate(R.layout.viewflipper, null);
		mFlipper = (ViewFlipper) layout.findViewById(R.id.viewFlipper);
		ImageView registrationRight_imagUrl = (ImageView) layout.findViewById(R.id.registrationRight_imagUrl);

		Registration registration = data.getRegistration();
		RegistrationRight registrationRight = registration.getRegistrationRight();
		UILUtils.displayImage(registrationRight.getImgUrl(), registrationRight_imagUrl);
		List<RegistrationLeft> registrationLeft = registration.getRegistrationLeft();
		for (int i = 0; i < registrationLeft.size(); i++) {
			View view = inflater.inflate(R.layout.item_viewflipper, null);
			ImageView imageUrl_registration = (ImageView) view.findViewById(R.id.imageUrl_registration);
			TextView title_registration = (TextView) view.findViewById(R.id.title_registration);
			String imgUrl = registrationLeft.get(i).getImgUrl();
			String title = registrationLeft.get(i).getTitle();
			UILUtils.displayImage(imgUrl, imageUrl_registration);
			title_registration.setText(title);
			mFlipper.addView(view);
		}
		// 设置进入与退出动画
		mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_in));
		mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_out));
		// 开始垂直滚动动画
		mFlipper.startFlipping();
		mListView.addHeaderView(layout);
	}

	// initHeadView5
	@SuppressLint("SimpleDateFormat")
	private void specials(Data data) {

		Specials specials = data.getSpecials();
		List<Content_> content = specials.getContent();
		View layout = inflater.inflate(R.layout.home_headview6, null);
		for (int i = 0; i < content.size(); i++) {
			TextView tv_promotionName = (TextView) layout.findViewById(R.id.tv_promotionName);
			TextView tv_title = (TextView) layout.findViewById(R.id.tv_title);
			TextView tv_promotionPrice = (TextView) layout.findViewById(R.id.tv_promotionPrice);
			TextView tv_promotionDiscount = (TextView) layout.findViewById(R.id.tv_promotionDiscount);
			CountdownView tv_promotionDeadline_time = (CountdownView) layout
					.findViewById(R.id.tv_promotionDeadline_time);
			ImageView iv_content1 = (ImageView) layout.findViewById(R.id.iv_content1);
			ImageView iv_content2 = (ImageView) layout.findViewById(R.id.iv_content2);
			ImageView iv_content3 = (ImageView) layout.findViewById(R.id.iv_content3);
			// Content_ contents = content.get(i);
			if (i == 0) {
				tv_promotionName.setText(content.get(i).getPromotionName() + "");
				tv_title.setText(content.get(i).getTitle());
				tv_promotionPrice.setText("¥" + content.get(i).getPromotionPrice() + "起");
				tv_promotionDiscount.setText(content.get(i).getPromotionDiscount() + "");
				UILUtils.displayImage(content.get(i).getImgUrl(), iv_content1);
				if (content.get(i).getPromotionDeadline() != null) {
					// 得到系统当前时间
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String format = sdf.format(new java.util.Date());
					// 活动天数减去系统时间得到小时数(毫秒)
					long time = timeClock(content.get(i).getPromotionDeadline() + "") - timeClock(format);
					// 设置时间控件显示
					tv_promotionDeadline_time.start(time);
				} else {
					// 服务器设置时间为空时，时间控件隐藏
					tv_promotionDeadline_time.setVisibility(View.INVISIBLE);
				}
			} else if (i == 1) {
				UILUtils.displayImage(content.get(i).getImgUrl(), iv_content2);
			} else if (i == 2) {
				UILUtils.displayImage(content.get(i).getImgUrl(), iv_content3);
			}
		}
		mListView.addHeaderView(layout);
	}

	// 此方法为把时间年月日时分秒转换成毫秒
	@SuppressLint("SimpleDateFormat")
	private long timeClock(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long millionSeconds = 0;
		try {
			millionSeconds = sdf.parse(str).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return millionSeconds;
	}

	// initHeadview6
	private void logingift(Data data) {
		LoginGift loginGift = data.getLoginGift();
		View view = inflater.inflate(R.layout.home_headview4_logingift, null);
		ImageView iv_logingift = (ImageView) view.findViewById(R.id.logingift);
		UILUtils.displayImage(loginGift.getImgUrl(), iv_logingift);
		mListView.addHeaderView(view);
	}

	// initHeadview7
	private void hot(Data data) {

		HotRecommend hotRecommend = data.getHotRecommend();
		List<Service> services = hotRecommend.getServices();
		View view = inflater.inflate(R.layout.home_hot, null);
		ImageView image_hot1 = (ImageView) view.findViewById(R.id.image_hot1);
		ImageView image_hot2 = (ImageView) view.findViewById(R.id.image_hot2);
		ImageView image_hot3 = (ImageView) view.findViewById(R.id.image_hot3);
		ImageView image_hot4 = (ImageView) view.findViewById(R.id.image_hot4);
		ImageView image_hot5 = (ImageView) view.findViewById(R.id.image_hot5);
		ImageView image_hot6 = (ImageView) view.findViewById(R.id.image_hot6);
		ImageView image_hot7 = (ImageView) view.findViewById(R.id.image_hot7);
		ImageView image_hot8 = (ImageView) view.findViewById(R.id.image_hot8);
		for (int i = 0; i < services.size(); i++) {
			if (i == 0) {
				hot(services, image_hot1, i);
			} else if (i == 1) {
				hot(services, image_hot2, i);
			} else if (i == 2) {
				hot(services, image_hot3, i);
			} else if (i == 3) {
				hot(services, image_hot4, i);
			} else if (i == 4) {
				hot(services, image_hot5, i);
			} else if (i == 5) {
				hot(services, image_hot6, i);
			} else if (i == 6) {
				hot(services, image_hot7, i);
			} else if (i == 7) {
				hot(services, image_hot8, i);
			}
		}
		mListView.addHeaderView(view);
	}

	// 热门推荐
	private void hot(List<Service> services, ImageView image_hot1, int i) {
		UILUtils.displayImage(services.get(i).getImgUrl(), image_hot1);
	}

	private void initeresting() {
		View layout = inflater.inflate(R.layout.home_interesting, null);
		ViewPager iViewPager = (ViewPager) layout.findViewById(R.id.interestPager);
		ImageView btn1 = (ImageView) layout.findViewById(R.id.btn_mudidi);
		ImageView btn2 = (ImageView) layout.findViewById(R.id.btn_helpchoose);
		myIViewPagerAdapter = new MyInterestingViewPager(getChildFragmentManager());
		iViewPager.setAdapter(myIViewPagerAdapter);
		mListView.addHeaderView(layout);

	}

	private void found(Data data) {
		// 点我领卷
		SignIn signIn = data.getSignIn();
		// 结伴同行 精彩游记 出游榜 福利社
		FindBeauty findBeauty = data.getFindBeauty();
		List<Service_> services = findBeauty.getServices();
		List<FindBeautyRight> findBeautyRight = data.getFindBeautyRight();
		View layout = inflater.inflate(R.layout.home_headview9_found, null);
		ImageView iv_signIn = (ImageView) layout.findViewById(R.id.iv_signIn);
		ImageView findBeauty_imgUrl1 = (ImageView) layout.findViewById(R.id.findBeauty_imgUrl1);
		ImageView findBeauty_imgUrl2 = (ImageView) layout.findViewById(R.id.findBeauty_imgUrl2);
		ImageView findBeauty_imgUrl3 = (ImageView) layout.findViewById(R.id.findBeauty_imgUrl3);
		ImageView findBeauty_imgUrl4 = (ImageView) layout.findViewById(R.id.findBeauty_imgUrl4);
		TextView findBeauty_title1 = (TextView) layout.findViewById(R.id.home_found_title1);
		TextView findBeauty_title2 = (TextView) layout.findViewById(R.id.home_found_title2);
		TextView findBeauty_title3 = (TextView) layout.findViewById(R.id.home_found_title3);
		TextView findBeauty_title4 = (TextView) layout.findViewById(R.id.home_found_title4);
		for (int i = 0; i < services.size(); i++) {
			if (i == 0) {
				findBeauty(services, findBeauty_imgUrl1, findBeauty_title1, i);
			} else if (i == 1) {
				findBeauty(services, findBeauty_imgUrl2, findBeauty_title2, i);
			} else if (i == 2) {
				findBeauty(services, findBeauty_imgUrl3, findBeauty_title3, i);
			} else if (i == 3) {
				findBeauty(services, findBeauty_imgUrl4, findBeauty_title4, i);
			}
		}

		UILUtils.displayImage(signIn.getImgUrl(), iv_signIn);
		// 垂直滚动动画
		ViewFlipper flipper = (ViewFlipper) layout.findViewById(R.id.viewFlipper_findBeautyRight);
		for (int i = 0; i < findBeautyRight.size(); i++) {
			View item_flipper = inflater.inflate(R.layout.item_viewflipper_found, null);
			ImageView mImageView = (ImageView) item_flipper.findViewById(R.id.home_found_imageUrl);
			TextView mTextView = (TextView) item_flipper.findViewById(R.id.home_found_title);
			UILUtils.displayImage(findBeautyRight.get(i).getImgUrl(), mImageView);
			mTextView.setText(findBeautyRight.get(i).getTitle());
			flipper.addView(item_flipper);
		}
		// 设置动画属性
		flipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_out));
		// 开始垂直滚动动画
		flipper.startFlipping();

		mListView.addHeaderView(layout);

	}

	// 发现精彩
	private void findBeauty(List<Service_> services, ImageView findBeauty_imgUrl1, TextView findBeauty_title1, int i) {
		UILUtils.displayImage(services.get(i).getImgUrl(), findBeauty_imgUrl1);
		findBeauty_title1.setText(services.get(i).getTitle());
	}

	private ArrayList<ProductList> productListsI = new ArrayList<ProductList>();
	private Button btn_content1;
	private Button btn_content2;
	private Button btn_content3;
	private MyInterestingViewPager myIViewPagerAdapter;
	private MyAdapter dangjiAdapter;
	private TextView recommendForYou_content1;
	private TextView recommendForYou_content2;
	private TextView recommendForYou_content3;
	private TextView recommendForYou_content4;
	private TextView recommendForYou_content11;
	private TextView recommendForYou_content22;
	private TextView recommendForYou_content33;
	private TextView recommendForYou_content44;
	private MyAdapter layououtOver;
	private TextView line1;
	private TextView line2;
	private TextView line3;
	private TextView line4;
	private TextView line11;
	private TextView line22;
	private TextView line33;
	private TextView line44;

	// initHeadview9
	private void offlineInfo(Data data) {
		OfflineInfo offlineInfo = data.getOfflineInfo();
		View layout = inflater.inflate(R.layout.home_headview10, null);
		TextView title = (TextView) layout.findViewById(R.id.offlineInfo_titile);
		title.setText(offlineInfo.getTitle());
		mListView.addHeaderView(layout);
	}

	// TODO
	// private void getPosition(int position) {
	// modulesList.clear();
	// List<Module> modules = recommendForYouList.get(position).getModules();
	// modulesList.addAll(modules);
	// }

	private void initHeadView13() {
		View layout = inflater.inflate(R.layout.home_headview13, null);
		recommendForYou_content1 = (TextView) layout.findViewById(R.id.recommendForYou_content1);
		recommendForYou_content2 = (TextView) layout.findViewById(R.id.recommendForYou_content2);
		recommendForYou_content3 = (TextView) layout.findViewById(R.id.recommendForYou_content3);
		recommendForYou_content4 = (TextView) layout.findViewById(R.id.recommendForYou_content4);

		line1 = (TextView) layout.findViewById(R.id.line1);
		line2 = (TextView) layout.findViewById(R.id.line2);
		line3 = (TextView) layout.findViewById(R.id.line3);
		line4 = (TextView) layout.findViewById(R.id.line4);
		GridView mGridView = (GridView) layout.findViewById(R.id.dangji_gridview);
		dangjiAdapter = new MyAdapter() {

			@Override
			public View getView(int position, View arg1, ViewGroup arg2) {
				View view = inflater.inflate(R.layout.item_hidden_gridview_title, null);
				TextView mTextView = (TextView) view.findViewById(R.id.recommenet_title1);

				mTextView.setText(modulesList.get(position).getTitle());

				return view;
			}

			@Override
			public int getCount() {
				return modulesList.size();
			}
		};
		mGridView.setAdapter(dangjiAdapter);
		recommendForYou_content1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				recommendForYou_content1.setTextColor(0xff33BD61);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff222222);
				recommendForYou_content11.setTextColor(0xff33BD61);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff222222);
				line1.setBackgroundColor(0xff33BD61);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xffffffff);
				line11.setBackgroundColor(0xff33BD61);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xffffffff);
				mListView.setSelection(mListView.getHeaderViewsCount() - 1);
				dangji(0);
			}

		});
		recommendForYou_content2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mListView.setSelection(mListView.getHeaderViewsCount() - 1);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff33BD61);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff222222);
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff33BD61);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff222222);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xff33BD61);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xffffffff);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xff33BD61);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xffffffff);
				dangji(1);
			}

		});
		recommendForYou_content3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mListView.setSelection(mListView.getHeaderViewsCount() - 1);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff33BD61);
				recommendForYou_content4.setTextColor(0xff222222);
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff33BD61);
				recommendForYou_content44.setTextColor(0xff222222);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xff33BD61);
				line44.setBackgroundColor(0xffffffff);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xff33BD61);
				line4.setBackgroundColor(0xffffffff);
				dangji(2);
			}

		});
		recommendForYou_content4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mListView.setSelection(mListView.getHeaderViewsCount() - 1);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff33BD61);
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff33BD61);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xff33BD61);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xff33BD61);
				dangji(3);
			}
		});

		mListView.addHeaderView(layout);
	}

	// TODO
	private void layououtOver() {
		hidden = (LinearLayout) view.findViewById(R.id.linear_hidden);
		recommendForYou_content11 = (TextView) view.findViewById(R.id.recommendForYou_content1);
		recommendForYou_content22 = (TextView) view.findViewById(R.id.recommendForYou_content2);
		recommendForYou_content33 = (TextView) view.findViewById(R.id.recommendForYou_content3);
		recommendForYou_content44 = (TextView) view.findViewById(R.id.recommendForYou_content4);

		line11 = (TextView) view.findViewById(R.id.line1);
		line22 = (TextView) view.findViewById(R.id.line2);
		line33 = (TextView) view.findViewById(R.id.line3);
		line44 = (TextView) view.findViewById(R.id.line4);
		GridView mGridView = (GridView) view.findViewById(R.id.dangji_gridview);
		layououtOver = new MyAdapter() {

			@Override
			public View getView(int position, View arg1, ViewGroup arg2) {
				View view = inflater.inflate(R.layout.item_hidden_gridview_title, null);
				TextView mTextView = (TextView) view.findViewById(R.id.recommenet_title1);

				mTextView.setText(modulesList.get(position).getTitle());

				return view;
			}

			@Override
			public int getCount() {
				return modulesList.size();
			}
		};
		mGridView.setAdapter(layououtOver);
		recommendForYou_content11.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				recommendForYou_content11.setTextColor(0xff33BD61);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff222222);
				recommendForYou_content1.setTextColor(0xff33BD61);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff222222);
				line1.setBackgroundColor(0xff33BD61);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xffffffff);
				line11.setBackgroundColor(0xff33BD61);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xffffffff);
				dangji(0);
			}

		});
		recommendForYou_content22.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff33BD61);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff222222);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff33BD61);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff222222);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xff33BD61);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xffffffff);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xff33BD61);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xffffffff);
				dangji(1);
			}

		});
		recommendForYou_content33.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff33BD61);
				recommendForYou_content44.setTextColor(0xff222222);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff33BD61);
				recommendForYou_content4.setTextColor(0xff222222);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xff33BD61);
				line4.setBackgroundColor(0xffffffff);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xff33BD61);
				line44.setBackgroundColor(0xffffffff);
				dangji(2);
			}

		});
		recommendForYou_content44.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				recommendForYou_content11.setTextColor(0xff222222);
				recommendForYou_content22.setTextColor(0xff222222);
				recommendForYou_content33.setTextColor(0xff222222);
				recommendForYou_content44.setTextColor(0xff33BD61);
				recommendForYou_content1.setTextColor(0xff222222);
				recommendForYou_content2.setTextColor(0xff222222);
				recommendForYou_content3.setTextColor(0xff222222);
				recommendForYou_content4.setTextColor(0xff33BD61);
				line1.setBackgroundColor(0xffffffff);
				line2.setBackgroundColor(0xffffffff);
				line3.setBackgroundColor(0xffffffff);
				line4.setBackgroundColor(0xff33BD61);
				line11.setBackgroundColor(0xffffffff);
				line22.setBackgroundColor(0xffffffff);
				line33.setBackgroundColor(0xffffffff);
				line44.setBackgroundColor(0xff33BD61);
				dangji(3);
			}
		});

		mListView.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				int headerViewsCount = mListView.getHeaderViewsCount();

				if (firstVisibleItem >= headerViewsCount - 1) {
					hidden.setVisibility(View.VISIBLE);
				} else {
					hidden.setVisibility(View.GONE);
				}

			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

		});
	}

	class MyPagerAdpter extends FragmentStatePagerAdapter {

		public MyPagerAdpter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			String imgUrl = advertisesList.get(position).getImage();
			return new BannerHomeFragment(imgUrl);
		}

		@Override
		public int getCount() {
			return advertisesList.size();
		}

	}

	class MyInterestingViewPager extends FragmentPagerAdapter {

		public MyInterestingViewPager(FragmentManager fm) {

			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {

			return new InterestFragment(arg0);
		}

		@Override
		public int getCount() {

			return contentList.size() / 4;
		}

	}

}
